package com.scu.phototabuser.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;

import com.scu.phototabuser.App;
import com.scu.phototabuser.Consts;
import com.scu.phototabuser.data.source.local.User;

/**
 * Created by tianfei on 2017/4/9.
 */

public class PreferenceUtil {

    private static final String KEY_TOKEN ="token";
    private static final String KEY_CHOSEN_TYPE ="types";

    private static final String KEY_USER_ID = "user_id";
    private static final String KEY_USER_NAME = "user_name";
    private static final String KEY_USER_EMAIL = "user_email";
    private static final String KEY_USER_HEADER_URL = "user_header";
    private static final String KEY_USER_SIG = "user_sig";
    private static final String KEY_NEED_LOGIN ="NeedLogin";
    private static final String KEY_EXISTS_USER_CHOSEN_FAVORITE_TYPES ="fx0001";

    private static final String DEFAULT_HEADER_URI ="res://com.scu.phototabuser/drawable/logo";

    private static SharedPreferences preferences=
            App.context.getSharedPreferences(App.APP_NAME, Context.MODE_PRIVATE);
    private static SharedPreferences.Editor editor;


    public static void saveLoginState(boolean value){
        editor=preferences.edit();
        editor.putBoolean(KEY_NEED_LOGIN,value);
        editor.commit();
    }

    public static void saveStringValue(String key,String val){
        editor=preferences.edit();
        editor.putString(key,val);
        editor.commit();
    }

    public static void saveToken(String token){
        saveStringValue(KEY_TOKEN,token);
    }

    public static boolean getBoolValue(String key,boolean val){
        return preferences.getBoolean(key,val);
    }

    public static String getStringValue(String key,String val){
       return preferences.getString(key,val);
    }


    public static void saveUserChosenTypes(String val){
        editor=preferences.edit();
        editor.putBoolean(KEY_EXISTS_USER_CHOSEN_FAVORITE_TYPES,true);
        editor.putString(KEY_CHOSEN_TYPE,val);
        editor.commit();
    }



    public static boolean isExistUserChosenTypes(){
        return preferences.getBoolean(KEY_EXISTS_USER_CHOSEN_FAVORITE_TYPES,false);
    }

    public static String getUserChosenTypes(){
        return preferences.getString(KEY_CHOSEN_TYPE,"");
    }

    public static String getToken(){
        return preferences.getString(KEY_TOKEN,"");
    }

    public static boolean isNeedLogin(){
        return preferences.getBoolean(KEY_NEED_LOGIN,true);
    }

    public static void clearPreferenceState(){
        clearLoginState();
        clearUserChosenTypes();
    }

    private static void clearUserChosenTypes() {
        editor=preferences.edit();
        editor.putBoolean(KEY_EXISTS_USER_CHOSEN_FAVORITE_TYPES,false);
        editor.commit();
    }

    public static void clearLoginState(){
        editor=preferences.edit();
        editor.putBoolean(KEY_NEED_LOGIN,true);
        editor.commit();
    }

    public static void saveUserInfo(User user){
        editor=preferences.edit();
        editor.putInt(KEY_USER_ID,user.getId());
        editor.putString(KEY_USER_NAME,user.getName());
        editor.putString(KEY_USER_EMAIL,user.getEmail());
        editor.putString(KEY_USER_HEADER_URL,user.getHeaderUrl());
        editor.putString(KEY_USER_SIG,user.getSignature());
        editor.commit();
    }

    public static User getSavedUser(){
        User user=new User();
        user.setId(preferences.getInt(KEY_USER_ID,0));
        user.setName(preferences.getString(KEY_USER_NAME,"NULL"));
        user.setEmail(preferences.getString(KEY_USER_EMAIL,"NULL"));
        user.setHeaderUrl(preferences.getString(KEY_USER_HEADER_URL,DEFAULT_HEADER_URI));
        user.setSignature(preferences.getString(KEY_USER_SIG,"NULL"));
        return user;
    }

}
